package com.zking.web;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.zking.dao.domain.lj.Admin;
import com.zking.dao.domain.lj.Advertisement;
import com.zking.service.impl.lj.AdminServicelmpl;
import com.zking.service.lj.AdvertisementService;
import com.zking.utils.RedisUtil;

@Controller
public class AdminController {
	
	@Autowired
	AdminServicelmpl AdminServicelmpl;
	
	@Autowired
	AdvertisementService Adver;
	
	 @Autowired
	 private RedisUtil redisUtil;
	
	@RequestMapping("/index")
	public String index() {
		return "/WEB-INF/admin/index";
	}
	@RequestMapping("/head")
	public String indexHead() {
		return "/WEB-INF/admin/inc/head";
	}
	@RequestMapping("/foot")
	public String footfoot() {
		return "/WEB-INF/admin/inc/foot";
	}
	@RequestMapping("/left")
	public String left() {
		return "/WEB-INF/admin/inc/left";
	}
	@RequestMapping("/appointmentupdate")
	public String appointmentupdate() {
		return "/WEB-INF/admin/appointmentupdate";
	}
	@RequestMapping("banner")
	public String banner() {
		return "/WEB-INF/admin/banner";
	}
	@RequestMapping("banneradd")
	public String banneradd() {
		return "/WEB-INF/admin/banneradd";
	}
	@RequestMapping("changepwd")
	public String changepwd() {
		return "/WEB-INF/admin/changepwd";
	}
	@RequestMapping("connoisseur")
	public String connoisseur() {
		return "/WEB-INF/admin/connoisseur";
	}
	@RequestMapping("connoisseuradd")
	public String connoisseuradd() {
		return "/WEB-INF/admin/connoisseuradd";
	}
	@RequestMapping("connoisseurupdate")
	public String connoisseurupdate() {
		return "/WEB-INF/admin/connoisseurupdate";
	}
	@RequestMapping("log")
	public String log() {
		return "/WEB-INF/admin/log";
	}
	@RequestMapping("main")
	public String main() {
		return "/WEB-INF/admin/main";
	}
	//登录
		@RequestMapping("/getadmin")
		public ModelAndView getadmin(Admin admin, HttpServletRequest request) {
			ModelAndView modelAndView=new ModelAndView();
			Admin admins = AdminServicelmpl.getadmin(admin);
			if (admins != null) {
				modelAndView.addObject("admins", admins);
				request.getSession().setAttribute("admins", admins);
				modelAndView.setViewName("/WEB-INF/admin/index");
			}else{
				request.setAttribute("msg", "用户名或密码错误");
				modelAndView.setViewName("/WEB-INF/admin/log");
			}
			return modelAndView;
		}
	//增加一个会员
	@RequestMapping("/addAdmin")
	public String addAdmin(Admin admin) {
		int i = AdminServicelmpl.addAdmin(admin);
		if(i>0) {
			return "redirect:/getAdmin";
		}else {
			return "useradd";
		}
	}
//	//显示所有管理员
//	@RequestMapping("/getAdmin")
//	public String getAdmin(HttpServletRequest request) {
//		List<Admin> admins = AdminServicelmpl.getAdmin();
//		if(admins != null) {
//			request.setAttribute("admins", admins);
//			return "/WEB-INF/admin/user";
//		}else {
//			return "/WEB-INF/admin/user";
//		}
//	}
	
	//修改密码
	@RequestMapping("updateAdmin")
	public String updateAdmin(Admin admin) {
		int i  = AdminServicelmpl.updateAdmin(admin);
		if (i==1) {
			return "/WEB-INF/admin/log";
		}else {
			return "/WEB-INF/admin/log";
		}
	}
	
	//查询所有管理员并存入Redis中
	 @RequestMapping("/getAdmin")
	    public String getAdmin() {
		 List<Admin> admins = AdminServicelmpl.getAdmin();
			if (redisUtil.lGetListSize("admins")==0) {
				for (Admin admin : admins) {
					redisUtil.lSet("admins", JSONObject.toJSONString(admin));
				}
				return "redirect:/getAdminobj";
			}else {
				return "redirect:/getAdminobj";
			}
			 
	    }
	 //删除一条数据
	 @RequestMapping("/deleteAdmin")
	 private String deleteAdmin(Integer id) {
		 int i = AdminServicelmpl.deleteAdmin(id);
		 if (i==1) {
			 return "redirect:/deladminobj";
			 
		 }else {
			 return "redirect:/deladminobj";
		 }
	 }
	 //查询一条数据
	 @RequestMapping("/getADMIN")
	 private String getADMIN(Admin admin,HttpServletRequest request) {
		 Admin admins = AdminServicelmpl.getADMIN(admin);
		 if (admins != null) {
			 request.getSession().setAttribute("admins", admins);
			 return "/WEB-INF/admin/connoisseuradd";
		 }else {
			 request.setAttribute("msg", "没有查询到数据");
			 return "/WEB-INF/admin/user";
		 }
	 }
	 //分页查询
	 @RequestMapping("/getAdminobj")
	 public String getAdminobj(HttpServletRequest request) {
		 String pnS = request.getParameter("pn");
			
			int pn = 0;
			int limit = 5;
			try{
				pn = Integer.parseInt(pnS);
			} catch (Exception e) {
				pn = 1;
			}
			
			if(pn < 1){
				pn = 1;
			}
			
			int total = (int)redisUtil.lGetListSize("admins");
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}
			
			if(pn > pages){
				pn = pages;
			}
			
			int start = (pn - 1)*limit;//第一个参数
			limit =start+5; 
		 List<Object> admin= redisUtil.lGet("admins", start, limit-1);
		 List<Admin> admins = new ArrayList<Admin>();
		 for (Object object : admin) {
		 	admins.add(JSONObject.parseObject(object.toString(), Admin.class));
		 }
			if (!admins.isEmpty()) {
				request.setAttribute("admins", admins);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				return "/WEB-INF/admin/user";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "/WEB-INF/admin/user";
			}
	 }
	 
	//删除redis中的数据
		@RequestMapping("deladminobj")
		public String deladminobj() {
			redisUtil.del("admins");
			return "redirect:/getAdmin";
		}
	 
	@RequestMapping("/addADERT")
	public String addADERT(Advertisement advert,MultipartFile file) {
		try {
			/**
	         * 上传图片
	         */
	        //图片上传成功后，将图片的地址写到数据库
	        String filePath = "E:/upload";//保存图片的路径
	        //获取原始图片的拓展名
	        String originalFilename = file.getOriginalFilename();
	        //新的文件名字，使用uuid随机生成数+原始图片名字，这样不会重复
	        String newFileName = UUID.randomUUID()+originalFilename;
	        //封装上传文件位置的全路径，就是硬盘路径+文件名
	        File targetFile = new File(filePath,newFileName); 
	        //把本地文件上传到已经封装好的文件位置的全路径就是上面的targetFile
	        file.transferTo(targetFile);
	        advert.setPicture(newFileName);//文件名保存到实体类对应属性上
	       // System.out.println(complete);
		} catch (Exception e) {
			e.printStackTrace();
		}
		int i = Adver.addADERT(advert);
		if (i != 0) {
			return "redirect:/getadvertisements";
		}else {
			return "/WEB-INF/admin/banneradd";
		}
	}
	//显示所有广告信息
	@RequestMapping("lsetadvertisements")
	public String lsetadvertisements() {
		List<Advertisement> list = Adver.getadvertisements();
		if (redisUtil.lGetListSize("advertisements")==0) {
			//用for循环把每一个对象都存入一个Redis集合中
			for (Advertisement advertisement : list) {
				redisUtil.lSet("advertisements", JSONObject.toJSONString(advertisement));
			}
			return "redirect:/lgetAdvertisement";
		}else {
			return "redirect:/lgetAdvertisement";
		}
//		ModelAndView modelAndView = new ModelAndView();
//		if (list != null) {
//			modelAndView.addObject("list",list);
//			modelAndView.setViewName("/WEB-INF/admin/banner");
//		}else {
//			modelAndView.addObject("msg","没有查询到数据");
//			modelAndView.setViewName("/WEB-INF/admin/banner");
//		}
//		return modelAndView;
	}
	 @RequestMapping("/lgetAdvertisement")
	 public String lgetAdvertisement(HttpServletRequest request) {
		 String pnS = request.getParameter("pn");
			
			int pn = 0;
			int limit = 5;
			try{
				pn = Integer.parseInt(pnS);
			} catch (Exception e) {
				pn = 1;
			}
			
			if(pn < 1){
				pn = 1;
			}
			
			int total = (int)redisUtil.lGetListSize("advertisements");
			int pages = total / limit;
			if(total % limit != 0){
				pages++;
			}
			
			if(pn > pages){
				pn = pages;
			}
			
			int start = (pn - 1)*limit;//第一个参数
			limit =start+5; 
			//从Redis中取出集合
		 List<Object> admin= redisUtil.lGet("advertisements", start, limit-1);
		 List<Advertisement> list = new ArrayList<Advertisement>();
		 //用for循环把每一条数据转化为对象并存入list集合
		 for (Object object : admin) {
		 	list.add(JSONObject.parseObject(object.toString(), Advertisement.class));
		 }
			if (!list.isEmpty()) {
				request.setAttribute("list", list);
				request.setAttribute("pn", pn);
				request.setAttribute("limit", limit);
				request.setAttribute("pages", pages);
				return "/WEB-INF/admin/banner";
			}else {
				request.setAttribute("msg", "没有查询到数据");
				return "/WEB-INF/admin/banner";
			}
	 }
	 
	//删除redis中的数据
	 @RequestMapping("deladverobj")
	 public String deladverobj() {
		 redisUtil.del("advertisements");
		 return "redirect:/lsetadvertisements";
	 }
	 //删除一条广告
	 @RequestMapping("deleteadv")
	 public String deleteadv(Integer id) {
		 int i = Adver.deleteadv(id);
		 if (i==1) {
			 return "redirect:/deladverobj";
		}else {
			return "redirect:/deladverobj";
		}
	 }
	 
	@RequestMapping("user")
	public String user() {
		return "/WEB-INF/admin/user";
	}
	@RequestMapping("useradd")
	public String useradd() {
		return "/WEB-INF/admin/useradd";
	}
	@RequestMapping("vip")
	public String vip() {
		return "/WEB-INF/admin/vip";
	}
	@RequestMapping("vipadd")
	public String vipadd() {
		return "/WEB-INF/admin/vipadd";
	}
	@RequestMapping("vipadd2")
	public String vipadd2() {
		return "/WEB-INF/admin/vipadd2";
	}
	@RequestMapping("vipreset")
	public String vipreset() {
		return "/WEB-INF/admin/vipreset";
	}
	@RequestMapping("topic")
	public String topic() {
		return "/WEB-INF/admin/topic";
	}
	@RequestMapping("topicupdate")
	public String topicupdate() {
		return "/WEB-INF/admin/topicupdate";
	}
}
